//
//  ImageTitleButton.m
//  成品家
//
//  Created by YYY on 2017/3/6.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "ImageTitleButton.h"

@implementation ImageTitleButton

//UIButton封装
-(id)initWithFrame:(CGRect)frame style:(CPJButtonStyle)style imageSize:(CGSize)imageSize titleSize:(CGSize)titleSize imageStr:(NSString *)imageStr titleStr:(NSString *)titleStr fontSize:(CGFloat)fontSize titleColor:(UIColor *)titleColor space:(CGFloat)space
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.buttonImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
        self.buttonTitle = [[UILabel alloc]initWithFrame:CGRectZero];
        self.buttonTitle.text = titleStr;
        self.buttonTitle.textColor = titleColor;
        self.buttonTitle.font = [UIFont systemFontOfSize:fontSize];
        
        [self addSubview:self.buttonImage];
        [self addSubview:self.buttonTitle];
        
        if(style == 0)
        {
            //左图右字
            self.buttonImage.frame = CGRectMake(0, (self.height-imageSize.height)/2.0, imageSize.width, imageSize.height);
            self.buttonTitle.frame = CGRectMake(self.buttonImage.right+space, (self.height-titleSize.height)/2.0, titleSize.width, titleSize.height);
        }
        else if (style == 1)
        {
            //左字右图
            self.buttonTitle.frame = CGRectMake(0, (self.height-titleSize.height)/2.0, titleSize.width, titleSize.height);
            self.buttonImage.frame = CGRectMake(self.buttonTitle.right+space, (self.height-imageSize.height)/2.0, imageSize.width, imageSize.height);
            
        }
        else if (style == 2)
        {
            //上图下字
            self.buttonImage.frame = CGRectMake((self.width-imageSize.width)/2.0,0, imageSize.width, imageSize.height);
            self.buttonTitle.frame = CGRectMake((self.width-titleSize.width)/2.0, self.buttonImage.bottom+space, titleSize.width, titleSize.height);
        }
        else
        {
            //上字下图
            self.buttonTitle.frame = CGRectMake((self.width-titleSize.width)/2.0, 0, titleSize.width, titleSize.height);
            self.buttonImage.frame = CGRectMake((self.width-imageSize.width)/2.0,self.buttonTitle.bottom+space, imageSize.width, imageSize.height);
        }
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
